package banksystem;

/**
 *A screen that the user enters an amount of money into and deposits it into a 
 * corresponding account
 * @author Robert McCune
 * @date Jan 16 2013
 */
public class DepositFrame extends javax.swing.JFrame {

	private Account account;
	private Controller mController;
	
	/**
	 * Creates new form DepositFrame to deposit money into an account
	 * 
	 * @param ID The account number the frame will be depositing into
	 */
	public DepositFrame(Controller controller, Account acct) {
		initComponents();
		account = acct;
		mController = controller;
		balanceLabel.setText(balanceLabel.getText() +"$" + account.getBalance());
		errorLabel.setVisible(false);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        submitButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        depositTextField = new javax.swing.JTextField();
        depositAmmountLabel = new javax.swing.JLabel();
        balanceLabel = new javax.swing.JLabel();
        errorLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        submitButton.setText("Submit");
        submitButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                submitButtonMouseClicked(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });

        depositAmmountLabel.setText("Deposit Ammount:");

        balanceLabel.setText("Current Balance: ");

        errorLabel.setForeground(new java.awt.Color(204, 0, 0));
        errorLabel.setText("Error: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(submitButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 167, Short.MAX_VALUE)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(depositTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(depositAmmountLabel)
                            .addComponent(balanceLabel)
                            .addComponent(errorLabel))
                        .addGap(0, 88, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(balanceLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(depositAmmountLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(depositTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(errorLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(submitButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	/**
	 * If a valid string exists in the deposit text field, that amount is deposited and the next frame 
	 * is shown. Else it displays an error message.
	 * @param evt event that triggers the method
	 */
	private void submitButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_submitButtonMouseClicked
		String text;
		double amount;
		
		try{
			text = depositTextField.getText();
			try{
				amount = currencyToDouble(text);
				this.mController.DepositToAccount(amount);
				//mController.
			}catch(NumberFormatException e){
				errorLabel.setText("Error: Please Enter A Number");
				errorLabel.setVisible(true);
			}	
		}catch(NullPointerException e){
			errorLabel.setText("Error: Please Enter A Number");
			errorLabel.setVisible(true);
		}
	}//GEN-LAST:event_submitButtonMouseClicked

	private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
		this.mController.SaveAndRestart(this);
	}//GEN-LAST:event_cancelButtonMouseClicked

	/**
	 * Converts a string representing US currency into a double
	 * 
	 * @param str a string representing currency
	 * @return A double equivalent to the str
	 * @throws NumberFormatException
	 * @throws NullPointerException 
	 */
	private double currencyToDouble(String str) 
			throws NumberFormatException, NullPointerException
	{
		double num;
		
		if(str.matches("\\$?[0-9]*\\.?[0-9]{0,2}"))
		{
			str = str.replaceFirst("[$]", "");
			num = Double.valueOf(str);
		}else{
			throw new NumberFormatException("Invalid Currency");
		}
		
		return num;
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel balanceLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel depositAmmountLabel;
    private javax.swing.JTextField depositTextField;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables
}
